package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.UserVo;
import com.demo.entity.UsersVo;
import com.demo.exception.BusinessException;
import com.demo.service.IUserService;
import com.star.annotation.DictionaryConvert;
import com.star.annotation.DictionaryIgnore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制类
 *
 * @author: star
 * @date: 2020-07-23
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    IUserService iUserService;

    /**
     * 测试非字典
     *
     * @return
     */
    @RequestMapping(value = "demo", method = RequestMethod.GET)
    public String demo() {
        return iUserService.getUser().getName();
    }

    /**
     * 测试实体类
     *
     * @return
     */
    @RequestMapping(value = "demo1", method = RequestMethod.GET)
    public UserVo demo1() {
        return iUserService.getUser();
    }

    /**
     * 测试List集合
     *
     * @return
     */
    @RequestMapping(value = "demo2", method = RequestMethod.GET)
    public List<UserVo> demo2() {
        return iUserService.getUserList();
    }

    /**
     * 测试忽略
     *
     * @return
     */
    @DictionaryIgnore
    @RequestMapping(value = "demo3", method = RequestMethod.GET)
    public UserVo demo3() {
        return iUserService.getUser();
    }

    /**
     * 测试嵌套集合
     *
     * @return
     */
    @RequestMapping(value = "demo4", method = RequestMethod.GET)
    public UsersVo demo4() {
        return iUserService.getUsers();
    }

    /**
     * 测试controller忽略service转换
     *
     * @return
     */
    @DictionaryIgnore
    @RequestMapping(value = "demo5", method = RequestMethod.GET)
    public UserVo demo5() {
        return iUserService.getUserConvert();
    }

    /**
     * 测试Page
     *
     * @return
     */
    @RequestMapping(value = "demo6", method = RequestMethod.GET)
    public Page<UserVo> demo6() {
        List<UserVo> list = iUserService.getUserList();
        Page<UserVo> page = new Page<>();
        page.setRecords(list);
        page.setTotal(list.size());
        page.setCurrent(1L);
        return page;
    }

    /**
     * Map
     *
     * @return
     */
    @RequestMapping(value = "demo7", method = RequestMethod.GET)
    public Map<String,String> demo7() {
        Map<String,String> map = new HashMap<>();
        map.put("stat","1");
        map.put("stats","0");
        return map;
    }

    /**
     * 测试非字典
     *
     * @return
     */
    @RequestMapping(value = "demo8", method = RequestMethod.GET)
    public UserVo demo8() {
        UserVo user = new UserVo();
        user.setName("测试");
        return iUserService.getUserLog(user);
    }


    /**
     * 测试异常
     *
     * @return
     */
    @RequestMapping(value = "demo9", method = RequestMethod.GET)
    public void demo9() {
        throw new BusinessException("测试异常");
    }

}
